using System;
using System.Collections.Generic;
using Volo.Abp.Application.Dtos;
using TenantManagementService.Monitoring;

namespace TenantManagementService.Monitoring
{
    /// <summary>
    /// 告警记录DTO
    /// </summary>
    public class AlertDto : EntityDto<Guid>
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid? TenantId { get; set; }

        /// <summary>
        /// 告警规则ID
        /// </summary>
        public Guid AlertRuleId { get; set; }

        /// <summary>
        /// 告警级别
        /// </summary>
        public AlertLevel Level { get; set; }

        /// <summary>
        /// 告警状态
        /// </summary>
        public AlertStatus Status { get; set; }

        /// <summary>
        /// 告警标题
        /// </summary>
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 告警消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 触发时的指标值
        /// </summary>
        public decimal TriggeredValue { get; set; }

        /// <summary>
        /// 维度
        /// </summary>
        public Dictionary<string, string>? Dimensions { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 持续时间（秒）
        /// </summary>
        public int Duration { get; set; }

        /// <summary>
        /// 是否已通知
        /// </summary>
        public bool IsNotified { get; set; }

        /// <summary>
        /// 最后通知时间
        /// </summary>
        public DateTime? LastNotificationTime { get; set; }

        /// <summary>
        /// 通知次数
        /// </summary>
        public int NotificationCount { get; set; }

        /// <summary>
        /// 处理人
        /// </summary>
        public string? Assignee { get; set; }

        /// <summary>
        /// 处理时间
        /// </summary>
        public DateTime? HandledTime { get; set; }

        /// <summary>
        /// 处理备注
        /// </summary>
        public string? HandleNote { get; set; }

        /// <summary>
        /// 告警标签
        /// </summary>
        public Dictionary<string, string>? Tags { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime? LastModificationTime { get; set; }

        /// <summary>
        /// 告警级别文本
        /// </summary>
        public string LevelText => Level switch
        {
            AlertLevel.Info => "信息",
            AlertLevel.Warning => "警告",
            AlertLevel.Critical => "严重",
            AlertLevel.Emergency => "紧急",
            _ => "未知"
        };

        /// <summary>
        /// 告警状态文本
        /// </summary>
        public string StatusText => Status switch
        {
            AlertStatus.Active => "活跃",
            AlertStatus.Resolved => "已解决",
            AlertStatus.Ignored => "已忽略",
            AlertStatus.Suppressed => "暂停",
            _ => "未知"
        };

        /// <summary>
        /// 持续时间文本
        /// </summary>
        public string DurationText
        {
            get
            {
                var duration = Duration;
                if (duration < 60)
                    return $"{duration}秒";
                if (duration < 3600)
                    return $"{duration / 60}分{duration % 60}秒";
                var hours = duration / 3600;
                var minutes = (duration % 3600) / 60;
                return $"{hours}小时{minutes}分";
            }
        }

        /// <summary>
        /// 是否紧急
        /// </summary>
        public bool IsUrgent => Level == AlertLevel.Critical || Level == AlertLevel.Emergency;

        /// <summary>
        /// 是否长期未解决
        /// </summary>
        public bool IsLongRunning => Duration > 86400; // 超过24小时
    }
}